package com.pan.controller.Users;

import com.pan.aop.BusinessType;
import com.pan.aop.MyLog;
import com.pan.controller.tools.Methods;
import com.pan.controller.tools.R1;
import com.pan.domain.User;
import com.pan.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/applyuser")
@Tag(name = "ApplyUserController",description = "用户发申请类")
public class ApplyUserController {
    @Autowired
    private IUserService userService;

    Methods methods = new Methods();

    //用户查看自己的请求信息
    @Operation(summary = "用户查看自己的请求信息")
    @GetMapping
    @MyLog(title = "用户查看自己的请求信息",optParam = "无",businessType = BusinessType.SELECT)
    public R1 applyUser(HttpServletRequest request){
        //先获取Session域中的账号信息
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //通过用户名获取整个集合
        User user = userService.getByUsername(user1.getUsername());
        //再把此对象传入,依靠的是姓名查询
        if (userService.getOwn(user)!=null){
            return new R1(true,userService.getOwn(user));
        }else {
            return new R1(false,"未查询到申请！");
        }

    }

    //用户申请或修改部门
    @Operation(summary = "用户申请或修改部门")
    @PutMapping
    @MyLog(title = "用户申请或修改部门",optParam = "#{user}",businessType = BusinessType.INSERT)
    public R1 applyOrChange(@Parameter(description = "用户信息,为一个Json字符串",required = true) @RequestBody User user,
                            HttpServletRequest request){
        //先获取Session域中的账号信息
        HttpSession session = request.getSession();
        User user1 = (User) session.getAttribute("user");
        //比对选中需要修改的信息
        User user1_2 = userService.getByUsername(user1.getUsername());
        System.out.println("------------------------------------------------------------------------------");
        System.out.println(user1_2.getName());
        System.out.println(user.getName());
        System.out.println("------------------------------------------------------------------------------");
        if (user.getName().equals(user1_2.getName())){
            //发送申请
            return new R1(true,userService.applyOrChange(user));
        }else {
            return new R1(false,"请选择自己！");
        }

    }
}
